<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

    </el-row>

    <el-table v-loading="loading" :data="accountList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="会员" align="center" prop="memberMobile" />
      <el-table-column label="总收益" align="center" prop="totalAmount" />
      <el-table-column label="可用收益" align="center" prop="avaAmount" />
      <el-table-column label="冻结收益" align="center" prop="freeAmount" />
      <el-table-column label="推广收益" align="center" prop="promtTotalAmount" />
      <el-table-column label="订单提成" align="center" prop="orderComm" />
      <el-table-column label="首绑收益" align="center" prop="bindCarAmount" />
      <el-table-column label="创建时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <span class="el-dropdown-link">
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown" >
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-tickets"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['member:bill:list']"
                >账单</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-s-grid"
                  @click="handleOrder(scope.row)"
                  v-hasPermi="['order:order:list']"
                >推广订单
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-s-grid"
                  @click="handleVipOrder(scope.row)"
                  v-hasPermi="['order:order:list']"
                >推广VIP店长订单
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>

      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 账单 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" v-dialogdrag>
      <member-bill :memberId="memberId" :key="(new Date().getTime())"></member-bill>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 订单 -->
    <el-dialog :title="title" :visible.sync="orderOpen"  width="1300px">
      <order-list :agentId="agentId" :partnerId="partnerId" :key="(new Date().getTime())"></order-list>
    </el-dialog>
  </div>
</template>

<script>
import { listAccount, getAccount, delAccount, addAccount, updateAccount, exportAccount } from "@/api/member/account";
import memberBill from "../bill/index";

import orderList from '../../order/order/index'
export default {
  name: "Account",
  components: {memberBill,orderList},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 会员账户表格数据
      accountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      orderOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        memberId: undefined,
        totalAmount: undefined,
        avaAmount: undefined,
        freeAmount: undefined,
        promtTotalAmount: undefined,
        orderComm: undefined,
        bindCarAmount: undefined,
        mobile: undefined,
        gmtTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        memberId: [
          { required: true, message: "会员id不能为空", trigger: "blur" }
        ],
      },
      memberId: undefined,
      agentId: undefined,
      partnerId: undefined,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询会员账户列表 */
    getList() {
      this.loading = true;
      listAccount(this.queryParams).then(response => {
        this.accountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        memberId: undefined,
        totalAmount: undefined,
        avaAmount: undefined,
        freeAmount: undefined,
        promtTotalAmount: undefined,
        orderComm: undefined,
        bindCarAmount: undefined,
        version: undefined,
        gmtTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加会员账户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getAccount(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.memberId = this.form.memberId;
        this.title = "会员账单";
      });
    },
    /** 修改按钮操作 */
    handleOrder(row) {
      this.agentId = row.memberId;
      this.partnerId = undefined;
      this.orderOpen = true;
      this.title = "推广订单";
    },
    handleVipOrder(row) {
      this.agentId = undefined;
      this.partnerId = row.memberId;
      this.orderOpen = true;
      this.title = "推广VIP订单";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateAccount(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addAccount(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除会员账户编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAccount(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有会员账户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAccount(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
